package com.yotoodesign.dianba.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.util.Log;
import android.widget.Toast;

import com.yotoodesign.dianba.app.GlobalApplication;
import com.yotoodesign.dianba.bean.BaseData;
import com.yotoodesign.dianba.bean.Tag;

/*
 * 分页抽象类
 */
public abstract class Pager {
	public final static  int DEFAULT_PAGE_SIZE = 10;
	
	private boolean inited = false;
	/*
	 * 分页大小
	 */
	private int pageSize = 10;
	/*
	 * 页数
	 */
	private int pageCount = 0;
	/*
	 * 总数
	 */
	private int totalCount = 0;
	/*
	 * 当前页索引
	 */
	private int selectedIndex = -1;
	
	/*
	 * 数据列表
	 */
	private List list = new ArrayList();
	
	/*
	 * 初始化
	 */
	public void init(List list,int pageSize){
		selectedIndex = -1;
		this.list = list;
		totalCount = list.size();
		this.pageSize = pageSize;
		//计算页数
		if(pageSize==0){
			pageCount = 1;
			this.pageSize = totalCount;//重新设置大小
		}else{
			pageCount = totalCount/pageSize;
			int mode = totalCount % pageSize;
			//如果求模大于0，则页数加1
			if(mode>0){
				pageCount++;
			}
		}
		
		inited = true;
	}
	
	/*
	 * 首页数据
	 */
	public ArrayList first(){
		selectedIndex = -1;
		return next();
	}
	
	/*
	 * 上一页
	 */
	public ArrayList prior(){
		ArrayList result = new ArrayList();
		
		if(inited == false) {
			Toast.makeText(GlobalApplication.getMainActivity(), "请先初始化数据！", Toast.LENGTH_SHORT).show();
			return result;
		}
		
		selectedIndex--;
		if(selectedIndex<0){
			selectedIndex = 0;
			Toast.makeText(GlobalApplication.getMainActivity(), "没有数据了", Toast.LENGTH_SHORT).show();
			return result;
		}
		
		int startIndex = selectedIndex*pageSize;
		int endIndex = startIndex + pageSize;
		
		if(endIndex>list.size()){
			endIndex = list.size();
		}
		
		for(int i = startIndex;i<endIndex;i++){
			result.add(list.get(i));
		}
		
		return result;
	}
	/*
	 * 下一页
	 */
	public ArrayList next(){
		ArrayList result = new ArrayList();
		
		if(inited == false) {
			Toast.makeText(GlobalApplication.getMainActivity(), "请先初始化数据！", Toast.LENGTH_SHORT).show();
			return result;
		}
		
		selectedIndex++;
		if(selectedIndex+1>pageCount){
			selectedIndex = pageCount-1;
			if(pageCount==0){
				Toast.makeText(GlobalApplication.getMainActivity(), "没有找到数据", Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(GlobalApplication.getMainActivity(), "没有更多数据了", Toast.LENGTH_SHORT).show();
			}
			return result;
		}
		
		int startIndex = selectedIndex*pageSize;
		int endIndex = startIndex + pageSize;
		
		if(endIndex>list.size()){
			endIndex = list.size();
		}
		
		
		for(int i = startIndex;i<endIndex;i++){
			result.add(list.get(i));
		}
		
		return result;
	}
	
	public void reset(){
		selectedIndex = -1;
	}
	
	/*
	 * 根据标签查找数据
	 */
	public List<BaseData> findByTag(Tag tag){
		List<BaseData> result = new ArrayList<BaseData>();
		
		if(inited == false) {
			Toast.makeText(GlobalApplication.getMainActivity(), "请先初始化数据！", Toast.LENGTH_SHORT).show();
			return result;
		}
		
		for(Iterator<BaseData> it = getList().iterator();it.hasNext();){
			BaseData data = it.next();
			List<Tag> tags = data.getTags();
			for(Iterator<Tag> itTag = tags.iterator();itTag.hasNext();){
				Tag t = itTag.next();
				if(t.getName().equals(tag.getName())){
					result.add(data);
				}
			}
		}
		return result;
	}
	/*
	 * 根据标签查找数据，多个标签
	 */
	public List<BaseData> findByTags(List<Tag> tags){
		List<BaseData> result = new ArrayList<BaseData>();
		
		if(inited == false) {
			Toast.makeText(GlobalApplication.getMainActivity(), "请先初始化数据！", Toast.LENGTH_SHORT).show();
			return result;
		}
		
		String tagStr = ",";
		for(Iterator<Tag> it = tags.iterator();it.hasNext();){
			tagStr += it.next().getName()+",";
		}
		
		for(Iterator<BaseData> IT = getList().iterator();IT.hasNext();){
			BaseData data = IT.next();
			List<Tag> ts = data.getTags();
			for(Iterator<Tag> itTag = ts.iterator();itTag.hasNext();){
				Tag t = itTag.next();
				if(tagStr.indexOf(","+t.getName()+",")!=-1){
					result.add(data);
					break;
				}
			}
		}
		
		return result;
	}

	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public int getSelectedIndex() {
		return selectedIndex;
	}
	public void setSelectedIndex(int selectedIndex) {
		this.selectedIndex = selectedIndex;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public boolean isInited() {
		return inited;
	}

	public void setInited(boolean inited) {
		this.inited = inited;
	}
	
}
